<?php
/**
 * @特惠赚
 * Author: 小谢
 * Date: 2018-3-7
 */
namespace app\admin\controller;
use think\facade\Session;
use think\facade\View;
use think\Db;
use think\Cache;

class System extends Base
{
    
    //分菜单层级
	public function refreshMenu(){
		$pmenu = $arr = array();
		$rs = M('system_module')->where('level>1 AND visible=1')->order('mod_id ASC')->select();
		foreach($rs as $row){
			if($row['level'] == 2){
				$pmenu[$row['mod_id']] = $row['title'];//父菜单
			}
		}

		foreach ($rs as $val){
			if($row['level']==2){
				$arr[$val['mod_id']] = $val['title'];
			}
			if($row['level']==3){
				$arr[$val['mod_id']] = $pmenu[$val['parent_id']].'/'.$val['title'];
			}
		}
		return $arr;
	}
   
    public function ajax_get_action()
    {
     	$control = I('controller');
     	$advContrl = get_class_methods("app\\admin\\controller\\".str_replace('.php','',$control));
     	$baseContrl = get_class_methods('app\admin\controller\Base');
     	$diffArray  = array_diff($advContrl,$baseContrl);
     	$html = '';
     	foreach ($diffArray as $val){
     		$html .= "<option value='".$val."'>".$val."</option>";
     	}
     	exit($html);
    }
     
    /**
    *@brief 权限列表
    */
    public function right_list(){
     	$name = I('name');
     	if(!empty($name)) {
     	    $condition['name|right'] = array('like',"%$name%");
     	    $right_list = M('system_menu')->where($condition)->order('id desc')->select();
     	} else {
     	    $right_list = M('system_menu')->order('id desc')->select();

     	}
        $group = $this->groupList();
        View::share(["right_list"=>$right_list,"count"=>count($right_list),"group"=>$group]);
        return view();
    }
    
    //权限码操作
    public function edit_right(){
        $data = I('post.');
        $type = I('get.type');
     	if($data){
     		$data['right'] = implode(',',$data['right']);
     		if(!empty($data['id'])) {
     			M('system_menu')->where(array('id'=>$data['id']))->save($data);
     		} else {
     			if(M('system_menu')->where(array('name'=>$data['name']))->count()>0){
     				$this->error('该权限名称已添加，请检查',U('System/right_list'));
     			}
     			unset($data['id']);
     			M('system_menu')->add($data);
     		}
     		$this->success('操作成功',U('System/right_list'));
     		exit;
     	}

     	$id = I('id');
        //权限组
        $group = $this->groupList();
        $planPath = APP_PATH.'admin/controller';
        $planList = array();
        $dirRes   = opendir($planPath);
        while($dir = readdir($dirRes))
        {
            if(!in_array($dir,array('.','..','.svn')))
            {
                $planList[] = basename($dir,'.php');
            }
        }

     	if($id){
     		$info = M('system_menu')->where(array('id'=>$id))->find();
     		$info['right'] = explode(',', $info['right']);
            View::share(["planList"=>$planList,"group"=>$group,"type"=>$type,"info"=>$info]);
     	}
        View::share(["planList"=>$planList,"group"=>$group,"type"=>$type]);
        return view();
    }
    
    //删除权限码
    public function right_del(){
     	$id = I('del_id');
     	if(is_array($id)){
     		$id = implode(',', $id); 
     	}
     	if(!empty($id)){
     		$r = M('system_menu')->where("id in ($id)")->delete();
     		if($r){
                $this->ajaxReturn(1);
     		}else{
                $this->ajaxReturn('删除失败');
     		}
     	}else{
            $this->ajaxReturn('参数有误');
     	}
    }

    /**
    *@brief 批量删除权限码
    */
    public function delAll() {
        $act = I('post.act');
        $ids = I('post.ids');
        if($act == 'delAll') {
             $flag = M('system_menu')->where('id','in',$ids)->delete();
             if($flag) {
               $this->ajaxReturn(1);
            } 
            $this->ajaxReturn("删除失败");
        }
    }

}